package com.xysq.fragment;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.rockcent.action.AppAction;
import com.rockcent.action.CallbackListener;
import com.rockcent.action.impl.AppActionImpl;
import com.rockcent.model.CustCouponOrderBO;
import com.xysq.lemon.R;
import com.xysq.util.PropertiesUtil;
import com.xysq.util.ToastUtil;
import com.xysq.util.UserInfoKeeper;

public class ChangePriceDialogFragment extends DialogFragment implements DialogInterface.OnClickListener {
    public static final String TAG = "ChangePriceDialogFragment";

    private static final String ARG_ORDER = "order";

    private EditText priceEdt;
    private CustCouponOrderBO orderBO;
    private double price;
    private static AppAction appAction;
    private static FragmentActivity mActivity;

    public static ChangePriceDialogFragment newInstance(FragmentActivity activity, CustCouponOrderBO orderBO) {
        ChangePriceDialogFragment fragment = new ChangePriceDialogFragment();
        Bundle args = new Bundle();
        args.putSerializable(ARG_ORDER, orderBO);
        fragment.setArguments(args);
        mActivity = activity;
        appAction = AppActionImpl.getInstance(activity, PropertiesUtil.getXyapiAddress(), PropertiesUtil.getQkapiAddress());

        return fragment;
    }

    public ChangePriceDialogFragment() {
        // Required empty public constructor
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(getActivity().getResources().getString(R.string.btn_handle_price));
        builder.setView(createDialogView());
        builder.setCancelable(true);
        builder.setPositiveButton("确认", this);
        builder.setNegativeButton("取消", this);
        return builder.create();
    }

    private View createDialogView() {
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.dialog_change_price, null, false);
        priceEdt = (EditText) dialogView.findViewById(R.id.edt_price);
        priceEdt.requestFocus();
        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(priceEdt, 0);
        return dialogView;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            orderBO = (CustCouponOrderBO) getArguments().getSerializable(ARG_ORDER);
        }
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which) {
            case DialogInterface.BUTTON_POSITIVE:
                if (TextUtils.isEmpty(priceEdt.getText())) {
                    ToastUtil.showShort(mActivity, "价格不能为空");
                    return;
                }
                double price = Double.valueOf(priceEdt.getText().toString());
                if (price < 0.01) {
                    ToastUtil.showShort(mActivity, "价格必须大于0.01");
                    return;
                }
                if (price > orderBO.getTotal()) {
                    ToastUtil.showShort(mActivity, "价格不能大于当前订单总价");
                    return;
                }
                if (String.valueOf(price).indexOf(".") != -1) {
                    if (String.valueOf(price).substring(String.valueOf(price).indexOf(".") + 1).length() > 2) {
                        ToastUtil.showShort(mActivity, "最多只能输入两位小数");
                        return;
                    }
                }
                orderBO.setPreferentialPrice(price);
                appAction.changeOrderPrice(UserInfoKeeper.readCustomerId(mActivity), orderBO.getOrderId(), price, new CallbackListener<Boolean>() {
                    @Override
                    public void onSuccess(Boolean data) {
                        if (data) {
                            ToastUtil.showShort(mActivity, "价格修改成功");
                        } else {
                            ToastUtil.showShort(mActivity, "价格修改失败");
                        }
                        dismiss();
                        return;
                    }

                    @Override
                    public void onFailure(String errorCode, String message) {
                        ToastUtil.showShort(mActivity, message);
                        dismiss();
                        return;
                    }
                });

                break;
            case DialogInterface.BUTTON_NEGATIVE:
                dismiss();
                break;
            default:
                throw new AssertionError();
        }
    }

}
